import { contextBridge, ipcRenderer  } from 'electron'

const electronApi = {
  openDir: () => ipcRenderer.invoke('dialog:openDir'),
  openFile: (extensions) => ipcRenderer.invoke('dialog:openFile', extensions),
  getMavenHome: () => ipcRenderer.invoke('file:getMaveHome'),
  isValidPath: (path) => ipcRenderer.invoke('file:isValidPath', path),
  packing: (type, params) => ipcRenderer.invoke('truck:packing', type, params),
  onPackingLog: (callback) => ipcRenderer.on('truck:onPackingLog', (_event, value) => callback(value)),
  unpacking: (type, params) => ipcRenderer.invoke('truck:unpacking', type, params),
  onUnPackingLog: (callback) => ipcRenderer.on('truck:onUnPackingLog', (_event, value) => callback(value)),
  onUnPackingProgress: (callback) => ipcRenderer.on('truck:onUnPackingProgress', (_event, value) => callback(value)),
}
// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electronAPI', electronApi)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electronAPI = electronApi
  window.api = api
}
